home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Bad code
- Date: 29 Mar 1996 14:00:25 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4jhmhpINNps7@anvil.ugrad.cs.ubc.ca>
- References: <4jhau8$hrl@hermes.oanet.com>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
- Keywords: Problems
-
- In article <4jhau8$hrl@hermes.oanet.com>,
- <scorpion@portal.connect.ab.ca> wrote:
- > I need to know why a piece of my C code doesn't work .
- >it does like:
- >
- > Object.Vertices+i.X = (long) .....
- > Object.Vertices+i.Y = (long).......
- > Object.Vertices+i.Z=(long) ........
- >
- > Vertices is a pointer to a struct with {long X,Y,Z} that
- >has been typedef'ed. i is an index vatiable that is added to the
- >pointer to make is point to the next {long X,Y,Z} typedef'ed
- >struct . When I go to compile , it says "not a struct or union
- >type".
-
- Object.Vertices is NOT a struct or union type, so you can't use the '.'
- operator to access members in it. It is a pointer to a structure. Therefore you
- have to dereference the pointer before accessing a member:
-
- (*Object.Vertices+i).x = (long) ....
-
- This is a bit clumsy, which is why most programmers use the following
- equivalent notation:
-
- (Object.Vertices+i)->X = ...
-
-
- When I parenthesize Vertices+i , I get an "Identified
-
- That's because (Vertices+i) is not a member of Object structure. It is a
- pointer expression. You cannot write Object.(Vertices + i). An identifier is
- expected after the ``Object.'' to name the member.
-
-
- You _must_ parenthesize it as I did above, because the + operator has a low
- precedence compared to . or ->. If you don't, the compiler will think you are
- trying to do:
-
- (Object.Vertices) + (i->X)
-
- Which is illegal, since i is (presumably) an integer and not a pointer to a
- structure.
- --
-
-